<?php
/* [DoYouHaoBaby!] (C)Dianniu From 2010.
   Gtk视图($)*/

!defined('DYHB_PATH')&& exit;

class ViewGtk extends View implements IViewGtk{

	static $_oShareGlobalTemplate;
	static public $_bWithInTheSystem=FALSE;
	private $_sTemplateFile;
	private $_sWidgetId;
	private $_oWndWidget;
	private $_bSetModelChangedByTemplateControllerChanging=true;
	private $_bEnableView=true;

	public function __construct($sName,$sTemplateFile=null,$sWidgetId=null,IViewContainer $oParent=null,TemplateGtkGlade $oTemplate=null){
		$this->_sTemplateFile=$sTemplateFile;
		$this->_sWidgetId=$sWidgetId;
		if($oTemplate){// 创建/设置 Template对象
			$this->setTemplate($oTemplate);
		}
		else{
			$this->setTemplate(self::createShareTemplate());
		}
		parent::__construct($sName,$oParent);
	}

	public function display( $GladeFile='',$sWidgetId='',array $arrObjectForSignal=array(),$bDisplay=true){
		if(!$this->_bEnableView){// 是否启用视图
			return;
		}
		$this->_nRuntime= E::getMicrotime();
		if(empty($GladeFile)) $GladeFile=&$this->_sTemplateFile;// 从视图构造器获取
		$GladeFile=$GladeFile;// 定位模板路径
		if(!is_file($GladeFile)){
			$GladeFile=$this->parseTemplateFile($GladeFile);
		}
		$this->_sTemplateFile=&$GladeFile;
		if(empty($sWidgetId)){
			$sSuffix='.glade';
			if(array($this->_sTemplateFile)){
				$sWidgetId=$this->_sTemplateFile['file'];
			}
			elseif(is_string($this->_sTemplateFile)){
				$sWidgetId=&$this->_sTemplateFile;
			}
			else{
				A::ASSERT_(0);
			}
			$sWidgetId=rtrim($sWidgetId, $sSuffix);
		}
		$this->_sWidgetId=&$sWidgetId;
		$_oWndWidget=$this->create($arrObjectForSignal);

		if($bDisplay){
			if($bDisplay===true){
				$_oWndWidget->show_all();
			}
			else{
				$_oWndWidget->show();
			}
			Gtk::main();
		}
		else{
			return $_oWndWidget;
		}
		exit();
	}

	static public function createShareTemplate(){
		if(!self::$_oShareGlobalTemplate){
			self::$_oShareGlobalTemplate=new TemplateGtkGlade();
		}
		return self::$_oShareGlobalTemplate;
	}

	public function getWidget(){
		return $this->_oWndWidget;
	}

	public function setWidget(gtkWidget $oWidget){
		$oOldValue=$this->_oWndWidget;
		$this->_oWndWidget=$oWidget;
		return $oOldValue;
	}

	public function getCreator(){
		$wndWidget=$this->getWidget();
		A::INSTANCE($wndWidget,'GtkWidget');
		$oWidgetCreator=$wndWidget->get_data('creator');
		A::INSTANCE($oWidgetCreator,'TemplateGtkWidgetCreator');
		return $oWidgetCreator;
	}

	public function getChildWidget($sWidgetId){
		A::STRING($sWidgetId);
		$wndWidget=$this->getWidget();
		A::INSTANCE($wndWidget,'GtkWidget');
		$oCreator=TemplateGtkGlade::getWidgetCreator($wndWidget);
		$arrObjectForSignal['TheView']=$this;// 可用于 响应 signal 事件的 对象
		$arrObjectForSignal['TheController']=$this->getParController();
		$arrObjectForSignal['TheModel']=$this->getModel();
		try{
			return $oCreator->getWidget($sWidgetId,$arrObjectForSignal);
		}
		catch(Exception $oE){
			if($oE->getCode()===TemplateGtkWidgetCreator::EXCE_ID_NOWIDGET){// 指定 id 的窗体不存在
				return null;
			}
			else{// 其他异常，继续向外抛出
				throw $oE;
			}
		}
	}

	public function create($arrObjectForSignal=array()){
		$oTemplate=$this->getTemplate();
		A::INSTANCE($oTemplate,'TemplateGtkGlade');
		$arrObjectForSignal['TheView']=$this;// 可用于 响应 signal 事件的 对象
		$arrObjectForSignal['TheController']=$this->getParController();
		if(self::$_bWithInTheSystem){
			Template::in(true);
		}
		$this->_oWndWidget=$oTemplate->create($this->_sTemplateFile,$this->_sWidgetId,$arrObjectForSignal);// 创建窗体
		if(self::$_bWithInTheSystem){
			Template::in(false);
		}
		self::in(false);// 还原状态
		A::INSTANCE($this->_oWndWidget,'GtkWidget');
		return $this->_oWndWidget;
	}

	static public function withInTheSystem($bWithInTheSystem=false){
		 $bOldValue=self::$_bWithInTheSystem;
		 self::$_bWithInTheSystem=$bWithInTheSystem;
		 return $bOldValue;
	}

	static public function in($bWithInTheSystem=false){
		 return self::withInTheSystem($bWithInTheSystem);
	}

	public function showView($bShow=true,$bForAllChildren=false){
		$oWndWidget=$this->getWidget();
		if(!$oWndWidget){$oWndWidget=$this->create();}
		$oWndWidget->set_visible($bShow,$bForAllChildren);
	}

	public function setImageBackground($sImagePath){
		try {// 载入图像
			$oPixbuf=GdkPixbuf::new_from_file($sImagePath);
		}
		catch(Exception $oE){
			$sMessage= $oE->getMessage();
			$sMessage. "\r\n Image {$sImagePath} Can't load.\r\n";
			G::echoMessage('',$sMessage);
			return;
		}
		list($oPixmap,)= $oPixbuf->render_pixmap_and_mask(255);
		$oWndWindow=$this->getWidget();// 将图像作为背景样式 设置给窗口
		$oStyle=$oWndWindow->get_style();
		$oStyle=$oStyle->copy();
		$oStyle->bg_pixmap[Gtk::STATE_NORMAL]=$oPixmap;
		$oWndWindow->set_style($oStyle);
	}

	public function destroyWindow(){
		$oWndWindow=$this->getWidget();
		$oWndWindow->destroy();
	}

}
